1
|
|
|
import { Selection } from 'prosemirror-state'; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Returns a command that tries to set the selected textblocks to the given node type with the given attributes. |
5
|
|
|
* |
6
|
|
|
* Copied and adjusted from prosemirror-commands::setBlockType to not check for the node attributes |
7
|
|
|
*/ |
8
|
|
|
export function setBlockTypeNoAttrCheck(nodeType, attrs) { // eslint-disable-line import/prefer-default-export |
9
|
|
|
return function setBlockTypeNoAttrCheckDispatch(state, dispatch) { |
10
|
|
|
const { from, to } = state.selection; |
11
|
|
|
let applicable = false; |
12
|
|
|
state.doc.nodesBetween(from, to, (node, pos) => { |
13
|
|
|
if (applicable) return false; |
|
|
|
|
14
|
|
|
if (!node.isTextblock || node.type === nodeType) return true; |
|
|
|
|
15
|
|
|
const $pos = state.doc.resolve(pos); |
16
|
|
|
const index = $pos.index(); |
17
|
|
|
applicable = $pos.parent.canReplaceWith(index, index + 1, nodeType); |
18
|
|
|
return true; |
19
|
|
|
}); |
20
|
|
|
if (!applicable) return false; |
|
|
|
|
21
|
|
|
if (dispatch) dispatch(state.tr.setBlockType(from, to, nodeType, attrs).scrollIntoView()); |
|
|
|
|
22
|
|
|
return true; |
23
|
|
|
}; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a command that inserts a new paragraph before or after the node at the given position |
28
|
|
|
* |
29
|
|
|
* @param {int} pos position near which the paragraph should be inserted |
30
|
|
|
* @param {string} direction should be 'before' or 'after' |
31
|
|
|
* @return {function} |
32
|
|
|
*/ |
33
|
|
|
export function insertParagraphAtPos(pos, direction = 'after') { |
34
|
|
|
return function insertParagraphAtPosCommand(state, dispatch) { |
35
|
|
|
const $pos = state.doc.resolve(pos); |
36
|
|
|
const above = $pos.node(-1); |
37
|
|
|
const beforeOrAfter = direction !== 'after' ? $pos.index(-1) : $pos.indexAfter(-1); |
38
|
|
|
const type = above.contentMatchAt(beforeOrAfter).defaultType; |
39
|
|
|
|
40
|
|
|
if (!above.canReplaceWith(beforeOrAfter, beforeOrAfter, type)) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (dispatch) { |
45
|
|
|
const insertPos = direction !== 'after' ? $pos.before() : $pos.after(); |
46
|
|
|
const tr = state.tr.replaceWith(insertPos, insertPos, type.createAndFill()); |
47
|
|
|
tr.setSelection(Selection.near(tr.doc.resolve(insertPos), 1)); |
48
|
|
|
dispatch(tr.scrollIntoView()); |
49
|
|
|
} |
50
|
|
|
return true; |
51
|
|
|
}; |
52
|
|
|
} |
53
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.